package com.deng.common.dao.impl;

import java.sql.Connection;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

import com.deng.common.SQLHelper;
import com.deng.common.dao.IDataTable;
import com.deng.common.vo.DataTableColumnVo;
import com.deng.common.vo.DataTableVo;

public class DataTableImpl extends SQLHelper implements IDataTable {

	public List<DataTableVo> getAllTable() {
		try{
			List<DataTableVo> l = new ArrayList<DataTableVo>();
			Connection conn = getConnect();
			ResultSet rs = queryData(conn,
					"select * from sysObjects where xtype = 'U'	order by name");
			
			while(rs.next()){
				DataTableVo vo = new DataTableVo();
				vo.id = rs.getInt("id");
				vo.name = rs.getString("name");
				l.add(vo);
			}
			freeConnection(conn);
			return l;
		}catch(Exception ex){
			ex.printStackTrace();
		}
		return null;
	}

	public List<DataTableColumnVo> getTableColumn(int id) {
		try{
			List<DataTableColumnVo> l = new ArrayList<DataTableColumnVo>();
			Connection conn = getConnect();
			ResultSet rs = queryData(conn,
					"select * from sys.all_columns where object_id = "+id);
			
			while(rs.next()){
				DataTableColumnVo vo = new DataTableColumnVo();
				vo.object_id = id;
				vo.name = rs.getString("name");
				vo.data_type = getDataType(rs.getInt("system_type_id"));
				l.add(vo);
			}
			freeConnection(conn);
			return l;
		}catch(Exception ex){
			ex.printStackTrace();
		}
		return null;
	}
	private String getDataType(int x){
		switch(x){
			case 56:return "int";
			case 61:return "datetime";
			case 62:return "float";
			case 106:return "decimal";
			case 167:return "varchar";
			case 231:return "nvarchar";
			case 239:return "nchar";
			default:return x+""; 
		}
	}
}
